package dao

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"analysis-ss/models"
)

func QueryTeacherChild(teacherId, childName interface{}, start, length int) ([]models.Child, error) {
	var childs []models.Child
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                   distinct(c.id) as id,c.name,
                   c.sex,grade,
                    c.year,
                    c.month,
                    c.day
                   from
                   schedule_detail_child sc left join child c on c.id=sc.child_id where  sc.teacher_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}



	sqlStr = fmt.Sprint(sqlStr, "  order by c.year asc ,CAST(c.month as SIGNED) asc,CAST(c.day as SIGNED) asc  limit ", start, ",", length)
	_, err := o.Raw(sqlStr, teacherId).QueryRows(&childs)
	return childs, err
}

func QueryCenterChild(centerId,childName interface{},start,length int) ([]models.Child,error) {
	var childs []models.Child
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
                   distinct(c.id) as id, c.name, c.sex, c.grade, c.year, c.month, c.day
                   FROM
                   child c
                   inner join schedule_detail_child dc on dc.child_id=c.id
                   WHERE
                   c.center_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}
	sqlStr = fmt.Sprint(sqlStr, " order by c.year asc ,CAST(c.month as SIGNED) asc,CAST(c.day as SIGNED) asc  limit ", start, ",", length)

	_, err := o.Raw(sqlStr, centerId).QueryRows(&childs)
	return childs,err
}

// 根据老师ID查找老师名字
func QueryTeacherCentername(teacherId interface{}) (string, error) {
	var centerName string
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                  distinct(center_name)
                  from
                  t_schedule
                  where
                  teacher_id = ?;`
	err := o.Raw(sqlStr, teacherId).QueryRow(&centerName)
	return centerName, err
}

// 根据中心ID查找中心名字
func QueryCentername(centerId interface{}) (string, error) {
	var centerName string
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                  distinct(center_name)
                  from
                  t_schedule
                  where
                  center_id = ?;`
	err := o.Raw(sqlStr, centerId).QueryRow(&centerName)
	return centerName, err
}

// 根据小孩子Id查找孩子信息
func QueryChild(childId interface{}) (models.Child, error) {
	var child models.Child
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `SELECT
                   id, name, sex, grade, year, month, day
                   FROM
                   t_child
                   WHERE
                   id = ?;`
	err := o.Raw(sqlStr, childId).QueryRow(&child)
	return child, err
}

// 获得老师进来的总页码
func QueryteacherStudentListTotal(teacherId, childName interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
                   count(distinct(c.id))
                   from
                   schedule_detail_child sc left join child c on c.id=sc.child_id where  sc.teacher_id = ? `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}
	err := o.Raw(sqlStr, teacherId).QueryRow(&Total)
	return Total, err
}

// 获得老师进来的总页码
func QueryCenterChildTotal(centerId, childName interface{}) (int, error) {
	var Total int
	o := orm.NewOrm()
	o.Using("default")
	sqlStr := `select
	           count(distinct(c.id))
                   FROM
                   child c
                   inner join schedule_detail_child dc on dc.child_id=c.id
                   WHERE
                   c.center_id = ?  `

	if childName != "" {
		sqlStr = fmt.Sprint(sqlStr, ` and  c.name like "%`, childName, `%" `)
	}
	err := o.Raw(sqlStr, centerId).QueryRow(&Total)
	return Total, err
}
